
options(scipen=20)

library(MASS)
library(WhatIf)
library(RColorBrewer)
library(simcf)
library(verification)
library(foreign)
library(nnet)
library(xtable)
library(apsrtable)
library(ggplot2)
library(dplyr)
library(stargazer)
col <- brewer.pal(3,"Dark2")

anes<-read.dta("anes.dta")
anes<- anes %>% mutate(ai=protest+petition+don_rel+don_soc+community+meet+volunteer+official)

ncps<-read.dta("ncps.dta")
ncps<-ncps %>% mutate(ai=petition+community+meeting+org+letter+donate+dem+elect)

## discrimination predicts sense of injustice in the NCPS ##

m1<-lm(cpi~disc+nonwhite, data=ncps,weights=weight)

wncps<-ncps[ which(ncps$nonwhite==0), ]
m2<-lm(cpi~disc+nonwhite,data=wncps)

nwncps<-ncps[ which(ncps$nonwhite==1), ]
m3<-lm(cpi~disc+nonwhite,data=nwncps)

stargazer(m1,m2,m3, no.space=TRUE,
          single.row = FALSE,
          font.size = "footnotesize",
          covariate.labels=c("Discrimination","Nonwhite",
                             "Constant"),
          dep.var.labels.include = FALSE,
          model.numbers=FALSE,
          star.cutoffs = c(0.05, 0.01, 0.001),
          column.labels = c("Total","White", "Nonwhite"),
          dep.var.labels   = "")


## Discrimination instead of sense of injustice (each alongside one another) ##

# straight models #

# injustice -- same models as in the body of the manuscript #

m1<-lm(ai~prox2+pers+cpi+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+inc101+
         incmis+polint+church+republican+independent+mode+nonwhite, 
       data=ncps,weights=weight)

wncps<-ncps[ which(ncps$nonwhite==0), ]

m2<-lm(ai~prox2+pers+cpi+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+inc101+
         incmis+polint+church+republican+independent+mode, 
       data=wncps)

nwncps<-ncps[ which(ncps$nonwhite==1), ]

m3<-lm(ai~prox2+pers+cpi+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+inc101+
         incmis+polint+church+republican+independent+mode, 
       data=nwncps)

# set alongside discrimination #

m4<-lm(ai~prox2+pers+disc+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+inc101+
         incmis+polint+church+republican+independent+mode+nonwhite, 
       data=ncps,weights=weight)

m5<-lm(ai~prox2+pers+disc+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+inc101+
         incmis+polint+church+republican+independent+mode, 
       data=wncps)

m6<-lm(ai~prox2+pers+disc+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+inc101+
         incmis+polint+church+republican+independent+mode, 
       data=nwncps)

stargazer(m1,m2,m3,m4,m5,m6, no.space=TRUE,
          single.row = FALSE,
          font.size = "footnotesize",column.sep.width="1.5pt",
          covariate.labels=c("Proximal",
                             "Personal",
                             "Injustice",
                             "Discrimination",
                             "Efficacy",
                             "Female","Age: 18-34","Age: 65+",
                             "Education","20k-40k","40k-60k","60k-80k","80k-100k","100k+",
                             "Missing Inc","Political Int","Religiosity",
                             "Republican","Independent","Mode","Nonwhite",
                             "Constant"),
          dep.var.labels.include = FALSE,
          model.numbers=FALSE,
          star.cutoffs = c(0.05, 0.01, 0.001),
          column.labels = c("Total","White", "Nonwhite", "Total","White","Nonwhite"),
          dep.var.labels   = "")


###############
# INTERACTION #
###############

# injustice -- same models as in the body of the manuscript #

m1<-lm(ai~prox2*cpi+pers+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+inc101+
         incmis+polint+church+republican+independent+mode+nonwhite, 
       data=ncps,weights=weight)

wncps<-ncps[ which(ncps$nonwhite==0), ]

m2<-lm(ai~prox2*cpi+pers+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+inc101+
         incmis+polint+church+republican+independent+mode, 
       data=wncps)

nwncps<-ncps[ which(ncps$nonwhite==1), ]

m3<-lm(ai~prox2*cpi+pers+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+inc101+
         incmis+polint+church+republican+independent+mode, 
       data=nwncps)

# discrimination #

m4<-lm(ai~prox2*disc+pers+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+inc101+
         incmis+polint+church+republican+independent+mode+nonwhite, 
       data=ncps,weights=weight)


m5<-lm(ai~prox2*disc+pers+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+inc101+
         incmis+polint+church+republican+independent+mode, 
       data=wncps)

m6<-lm(ai~prox2*disc+pers+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+inc101+
         incmis+polint+church+republican+independent+mode, 
       data=nwncps)

summary(m6)
stargazer(m1,m2,m3, m4,m5,m6, no.space=TRUE,
          single.row = FALSE,
          font.size = "footnotesize",column.sep.width="1.5pt",
          order=c(1,2,3,22,23,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,24),
          covariate.labels=c("Proximal",
                             "Injustice",
                             "Discrimination",
                             "Prox*Injustice",
                             "Prox*Discrimination",
                             "Personal",
                             "Efficacy",
                             "Female","Age: 18-34","Age: 65+",
                             "Education","20k-40k","40k-60k","60k-80k","80k-100k","100k+",
                             "Missing Inc","Political Int","Religiosity",
                             "Republican","Independent","Mode","Nonwhite",
                             
                             "Constant"),
          dep.var.labels.include = FALSE,
          model.numbers = FALSE,
          star.cutoffs = c(0.05, 0.01, 0.001),
          column.labels = c("Total","White", "Nonwhite", "Total","White","Nonwhite"),
          dep.var.labels   = "")



#####################################
########## MEDIATION ANALYSIS #######
#####################################
library(mediation)

# using a sense of injustice -- models replicated from main manuscript
model.m<-lm(cpi~prox2+pers+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+
              inc101+incmis+polint+church+republican+independent+mode+nonwhite, 
            data=ncps,weights=weight)
model.y<-lm(ai~prox2+pers+cpi+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+
              inc101+incmis+polint+church+republican+independent+mode+nonwhite, 
            data=ncps, weights=weight)
out.1<-mediate(model.m, model.y, sims=5000, treat="prox2", mediator="cpi")
summary(out.1)
# white
model.m<-lm(cpi~prox2+pers+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+
              inc101+incmis+polint+church+republican+independent+mode, 
            data=wncps)
model.y<-lm(ai~prox2+pers+cpi+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+
              inc101+incmis+polint+church+republican+independent+mode, 
            data=wncps)
out.2<-mediate(model.m, model.y, sims=5000, treat="prox2", mediator="cpi")
summary(out.2)
# nonwhite
model.m<-lm(cpi~prox2+pers+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+
              inc101+incmis+polint+church+republican+independent+mode, 
            data=nwncps)
model.y<-lm(ai~prox2+pers+cpi+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+
              inc101+incmis+polint+church+republican+independent+mode, 
            data=nwncps)
out.3<-mediate(model.m, model.y, sims=5000, treat="prox2", mediator="cpi")
summary(out.3)

########################
# using discrimination #
########################

model.m<-lm(disc~prox2+pers+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+
              inc101+incmis+polint+church+republican+independent+mode+nonwhite, 
            data=ncps,weights=weight)
model.y<-lm(ai~prox2+pers+disc+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+
              inc101+incmis+polint+church+republican+independent+mode+nonwhite, 
            data=ncps, weights=weight)
out.4<-mediate(model.m, model.y, sims=5000, treat="prox2", mediator="disc")
summary(out.4)
# white
model.m<-lm(disc~prox2+pers+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+
              inc101+incmis+polint+church+republican+independent+mode, 
            data=wncps)
model.y<-lm(ai~prox2+pers+disc+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+
              inc101+incmis+polint+church+republican+independent+mode, 
            data=wncps)
out.5<-mediate(model.m, model.y, sims=5000, treat="prox2", mediator="disc")
summary(out.5)
# nonwhite
model.m<-lm(disc~prox2+pers+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+
              inc101+incmis+polint+church+republican+independent+mode, 
            data=nwncps)
model.y<-lm(ai~prox2+pers+disc+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+
              inc101+incmis+polint+church+republican+independent+mode, 
            data=nwncps)
out.6<-mediate(model.m, model.y, sims=5000, treat="prox2", mediator="disc")
summary(out.6)


### ANES for comparison ###
model.m<-lm(disc~prox2+pers+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+
              inc101+incmis+polint+church+republican+independent+nonwhite, 
            data=anes,weights=weight)
model.y<-lm(ai~prox2+pers+disc+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+
              inc101+incmis+polint+church+republican+independent+nonwhite, 
            data=anes, weights=weight)
out.0<-mediate(model.m, model.y, sims=5000,treat="prox2", mediator="disc")
summary(out.0)
# white
model.m<-lm(disc~prox2+pers+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+
              inc101+incmis+polint+church+republican+independent, 
            data=wanes,weights=weight)
model.y<-lm(ai~prox2+pers+disc+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+
              inc101+incmis+polint+church+republican+independent, 
            data=wanes, weights=weight)
out.0<-mediate(model.m, model.y, sims=5000,treat="prox2", mediator="disc")
summary(out.0)
# nonwhite
model.m<-lm(disc~prox2+pers+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+
              inc101+incmis+polint+church+republican+independent, 
            data=nwanes,weights=weight)
model.y<-lm(ai~prox2+pers+disc+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+
              inc101+incmis+polint+church+republican+independent, 
            data=nwanes, weights=weight)
out.0<-mediate(model.m, model.y, sims=5000,treat="prox2", mediator="disc")
summary(out.0)








